﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIObjects.TreeViewInterfaces;
#if SILVERLIGHT
using AbstraX.ClientInterfaces;
#else
using AbstraX.ServerInterfaces;
#endif
using System.Collections.ObjectModel;
using AbstraX.Contracts;
using MvvmTreeView;
using System.Windows;
using SL4PopupMenu;
using System.Diagnostics;

namespace AbstraX.ViewModels.ModelTree
{
    public class ModelTreeNodeAttribute : ModelTreeNodeBase
    {
        private IAttribute attribute;

        public ModelTreeNodeAttribute(IProviderClientService providerService, ITreeView treeView, IAttribute attribute) : base(providerService, treeView)
        {
            this.attribute = attribute;
            this.Tag = attribute;

            var root = attribute.Root;

#if HACK
            abstraXExtension = attribute.LoadExtension(attribute, this);
#endif
        }

        public override ContextMenu ContextMenu
        {
            get
            {
                ContextMenu contextMenu = null;
                ContextMenuItem buildMenuItem;

                RoutedEventHandler clickHandler = (sender, r) =>
                {
                    var menuItem = (PopupMenuItem)sender;
                    var invokeOperation = providerService.Build((string)menuItem.Tag);

                    invokeOperation.OnBuildCompleted += (sender2, e) =>
                    {
                        if (invokeOperation.HasException)
                        {
                            Debugger.Break();
                        }
                    };
                };

                buildMenuItem = new ContextMenuItem(null, "Build Middle Layer...", attribute.ID, ContextMenuItemType.View, clickHandler);

                if (abstraXExtension != null)
                {
                    contextMenu = abstraXExtension.ContextMenu;
                }

                if (contextMenu == null)
                {
                    contextMenu = new ContextMenu
                    {
                        Items = new List<IContextMenuItem>()
                        {
                            buildMenuItem
                        }
                    };
                }
                else
                {
                    contextMenu.Items.Add(buildMenuItem);
                }

                return contextMenu;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override IBase BaseObject
        {
            get 
            {
                return attribute;
            }
        }

        public override string Text 
        {
            get
            {
                return attribute.Name;
            }

            set
            {
                throw new NotImplementedException();
            }
        }

        public override void LoadChildren()
        {
            var attribute = (IAttribute)this.Tag;

#if SILVERLIGHT
            attribute.ChildrenLoaded += new ChildrenLoadedHandler(ChildrenLoaded);
            attribute.LoadChildren();
#endif
        }

        protected override void ChildrenLoaded(IBase parent)
        {
            this.RemoveSpinster();
            isChildrenLoaded = true;
        }

        public override List<string> AddNewShortCuts
        {
            get { throw new NotImplementedException(); }
        }

        public override void OnSelect()
        {
        }

        public override void AddNew(string shortCut)
        {
            throw new NotImplementedException();
        }

        public override void DragStarting(object sender, UIObjects.DragDropInterfaces.ItemDragEventArgs e)
        {
        }

        public override void DragCompleted(object sender, UIObjects.DragDropInterfaces.ItemDragEventArgs e)
        {
        }

        public override void ItemDropped(object sender, UIObjects.DragDropInterfaces.ItemDragEventArgs e, int insertionIndex)
        {
       }

        public override void GiveFeedback(object sender, UIObjects.DragDropInterfaces.GiveFeedbackEventArgs e)
        {
        }

        public override void DragEnter(object sender, UIObjects.DragDropInterfaces.DragEventArgs e)
        {
       }

        public override void DragLeave(object sender, UIObjects.DragDropInterfaces.DragEventArgs e)
        {
        }

        public override void DragOver(object sender, UIObjects.DragDropInterfaces.DragEventArgs e)
        {
        }

        public override float ChildOrdinal
        {
            get
            {
                return attribute.ChildOrdinal;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override bool? HasChildren
        {
            get 
            {
                return attribute.HasChildren;
            }
        }
    }
}
